package day06;

import day05.User;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebFilter({"*.cart","*.order","/shop/cart.jsp","/shop/myOrder.jsp","/shop/myOrders.jsp","/shop/order.jsp","/shop/success.jsp"})
public class CheckLoginFilter extends HttpFilter {

    @Override
    protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {

        System.out.println("CheckLoginFilter.doFilter()");

        User user= (User) request.getSession().getAttribute("user");

        if(user==null){
            response.sendRedirect(request.getContextPath()+"/shop/login.jsp");
            return;
        }

        chain.doFilter(request,response);

    }
}
